unit oProjectManager;

interface
uses oScene, oManager, oConfiguration, oProject, oWorldConfiguration,
  oProcessItem, OProcessObjectConstants, SysUtils, oLevel, oDataTypeConstants,
  oProcessObjects, Generics.Collections, Classes;

  type
      EProjectManager = class(Exception)
      end;

      TProjectManager = class(tManager)
      private
        aProject : TProject;
        aPedName, aLogName, aPhysName, aPathName,
        aArrivalGroup, aArrival : string;
        procedure saveAll;
        procedure loadDataFromManager(paLoadCode, paNamesCode, paInsertCode : integer);
        function saveDataFromManager(paSaveCode : integer; paPath : string) : TProcessItem;
        procedure insert(paList : TList<TMapping>; paCode : integer);
      public
        constructor Create; overload;
        constructor Create(paMaster : TManager); overload;
        function ProcessObject(paItem: TProcessItem): TProcessItem;  override;
        property Project : TProject read aProject write aProject;
        destructor Done;
      end;


implementation

{ TCatalogueManager }

constructor TProjectManager.Create;
begin
  aProject := TProject.Create;
  aPedName := '';
  aLogName := '';
  aPhysName := '';
  aPathName := '';
end;

constructor TProjectManager.Create(paMaster: TManager);
begin
   Create;
   aMaster := paMaster;
end;

destructor TProjectManager.Done;
begin
  aProject.Free;
  inherited;
end;

procedure TProjectManager.insert(paList: TList<TMapping>; paCode : integer);
var
  map : TMapping;
begin
for map in paList do
   aMaster.ProcessObject(TProcessItem.create(paCode,
      TString.Create(map.Key)));
end;

procedure TProjectManager.loadDataFromManager(paLoadCode, paNamesCode, paInsertCode : integer);
var
  sList : TStringList;
  item : TProcessItem;
  i : integer;
begin
   aMaster.ProcessObject(TProcessItem.create(paLoadCode, NIL));
   item := aMaster.ProcessObject(TProcessItem.create(paNamesCode, NIL));
   slist := (item.Data as TprocessingStrings).Strings;
   for i:= 0 to slist.Count -1 do
    aMaster.ProcessObject(TProcessItem.create(paInsertCode,
    TString.Create(slist[i])));
   item.Free;
end;

function TProjectManager.ProcessObject(paItem: TProcessItem):TProcessItem;
var
  inList, outList : TProcessingList;
  name, newname : TString;
  value : Tinteger;
  item : TProcessItem;
  slist : TStringlist;
  map: TMapping;
  xml : TProcessingXML;
begin
    name := nil;
    inList := nil;
    Result := nil;
    if paItem.Data is TProcessingList then
      inList := TProcessingList(paItem.Data)
    else
      if paItem.Data is TString then
        name := TString(paItem.Data)
      else
       if paItem.Data is TProcessingXML then
        xml := TProcessingXML(paItem.Data)
       else
        if paItem.Data <> nil then
            raise EProjectManager.Create('ProjectManager: Expected TProcessingList or TString or TProcessingXML instance');

    case paItem.Code of
      PO_PROJECT_HAS_PATH : begin
        if aProject.Path <> '' then
          Result := TProcessItem.create(PO_SUCCESS, TInteger.Create(PO_SUCCESS))
        else
          Result := TProcessItem.create(PO_SUCCESS, TInteger.Create(PO_FAILURE));
      end;

      PO_PROJECT_GET_NAME : begin
        Result := TProcessItem.create(PO_SUCCESS, TString.Create(Project.Name));
      end;

      PO_PROJECT_DELETE : begin
        if DirectoryExists(aProject.Path) then
         begin
          aProject.delete;
          Result := TProcessItem.create(PO_SUCCESS, NIL);
         end
        else
          Result := TProcessItem.create(PO_FAILURE, NIL);
      end;

      PO_PROJECT_DELETE_PHYS_LAYER : begin
         outList := TProcessingList.Create;
         outList.addParameter(TString.Create(name.String_));
         outList.addParameter(TString.Create(TProject.getFileName(name.String_,
          Project.PhysicalLayers)));
         outList.addParameter(TInteger.Create(PHYS_LAYER));
         aMaster.ProcessObject(TProcessItem.create(PO_SCENE_DELETE_LAYER,
                               outList));
         aProject.deletePhysLayer(name.String_);
         aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_WITH_PATH, NIL));
         Result := TProcessItem.create(PO_SUCCESS, nil);
      end;

      PO_PROJECT_DELETE_LOG_LAYER : begin
         outList := TProcessingList.Create;
         outList.addParameter(TString.Create(name.String_));
         outList.addParameter(TString.Create(TProject.getFileName(name.String_,
          Project.LogicalLayers)));
         outList.addParameter(TInteger.Create(LOG_LAYER));

         aMaster.ProcessObject(TProcessItem.create(PO_SCENE_DELETE_LAYER,
                               outList));
         aProject.deleteLogLayer(name.String_);
         aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_WITH_PATH, NIL));
         Result := TProcessItem.create(PO_SUCCESS, nil);
      end;

      PO_PROJECT_SAVE_SCENE : begin
        try
          inList := TProcessingList.Create;
          if aProject.Path = '' then
            raise EProjectManager.Create('Project path not set');

          inList.addParameter(TString.Create(aProject.Path));
          inList.addParameter(TProcessingMap.Create(Project.PhysicalLayers));
          inList.addParameter(TProcessingMap.Create(Project.LogicalLayers));
          aMaster.ProcessObject(TProcessItem.create(PO_SCENE_SAVE,
          inList));
          Result := TProcessItem.create(PO_SUCCESS, nil);
        except
          on e : EProjectManager do Result := TProcessItem.create(PO_FAILURE, nil);
        end;
      end;

      PO_PROJECT_SAVE_LAYER : begin
         outList := TProcessingList.Create;
         outList.addParameter(TString(inList.aParameters[0]));
         outList.addParameter(TInteger(inList.Parameters[1]));
         if TInteger(inList.Parameters[1]).Integer_ = LOG_LAYER then
          outList.addParameter(TString.Create(Project.getLogLayerFileName(name.String_)))
         else
         outList.addParameter(TString.Create(Project.getPhysLayerFileName(name.String_)));
         aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_WITH_PATH, NIL));
         aMaster.ProcessObject(TProcessItem.create(PO_SCENE_SAVE_LAYER, outList));
      end;

      PO_PROJECT_LOAD_SCENE : begin
          try
          inList := TProcessingList.Create;
          if aProject.Path = '' then
            raise EProjectManager.Create('Project path not set');

          inList.addParameter(TString.Create(aProject.Path));
          inList.addParameter(TProcessingMap.Create(Project.PhysicalLayers));
          inList.addParameter(TProcessingMap.Create(Project.LogicalLayers));
          aMaster.ProcessObject(TProcessItem.create(PO_SCENE_LOAD,
            inList));
          Result := TProcessItem.create(PO_SUCCESS, nil);
          except
            on E : EProjectManager do Result := TProcessItem.create(PO_FAILURE, nil);
          end;
      end;

      PO_PROJECT_LOAD_PATHS : begin
        if name <> nil then begin
          item := aMaster.ProcessObject(TProcessItem.create(PO_LOADMAN_LOAD_DATA,
            TString.Create(Project.Path + '/paths/' +
            Project.getPathFileName(name.String_) + '.xml')));
          aPathName := name.String_;
          end
        else
          item := aMaster.ProcessObject(TProcessItem.create(PO_LOADMAN_LOAD_DATA,
            TString.Create(Project.Path + '/paths/' +
            Project.getPathFileName(aPathName) + '.xml')));

          if item.Code <> PO_FAILURE then
           begin
            aMaster.ProcessObject(TProcessItem.create(PO_PATHS_LOAD, TProcessingXML(item.Data)))
           end
          else
            raise EProjectManager.Create('Error on load paths');
      end;

      PO_PROJECT_GET_LAYERS : begin
        Result := TProcessItem.create(PO_SUCCESS,
                  TProcessingMap.Create(Project.PhysicalLayers));
      end;

      PO_PROJECT_GET_PEDESTRIAN_GROUP_ID : begin
        Project.PedestrianGroupId := project.PedestrianGroupId + 1;
        aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_WITH_PATH, nil));
        Result := TProcessItem.create(PO_SUCCESS, TInteger.Create(Project.PedestrianGroupId));
      end;

      PO_PROJECT_UPDATE_PHYS_LAYER : begin
        name := TString(inList.Parameters[0]);
        newname := TString(inList.Parameters[1]);
        if not Project.existsPhysLayer(newname.String_) then
          begin
            outlist := TProcessingList.Create;
            outList.addParameter(TString.Create(Project.getPhysLayerFileName(name.String_)));
            outList.addParameter(TInteger.Create(PHYS_LAYER));
            outList.addParameter(TString.Create((newname.String_)));

            outList.addParameter((inList.Parameters[2])); // level size
            outList.addParameter((inList.Parameters[3])); // sector count
            outList.addParameter((inList.Parameters[4])); // cell size

            aMaster.ProcessObject(TProcessItem.create(PO_SCENE_UPDATE_LAYER, outList));
            Project.updatePhysLayer(name.String_, newname.String_);
            aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_WITH_PATH, NIL));
            Result := TProcessItem.create(PO_SUCCESS, nil);
          end
         else Result := TProcessItem.create(PO_FAILURE, nil);
      end;

      PO_PROJECT_UPDATE_PEDESTRIAN : begin
        name := inlist.Parameters[0];
        newname := inlist.aParameters[1];
        Project.updatePedestrian(name.String_, newname.String_);
      end;

      PO_PROJECT_UPDATE_PATH : begin
        name := inlist.Parameters[0];
        newname := inlist.aParameters[1];
        Project.updatePath(name.String_, newname.String_);
      end;

      PO_PROJECT_UPDATE_LOG_LAYER : begin
        name := TString(inList.Parameters[0]);
        newname := TString(inList.Parameters[1]);
        if not Project.existsLogLayer(newname.String_) then
          begin
            outlist := TProcessingList.Create;
            outList.addParameter(TString.Create(Project.getLogLayerFileName(name.String_)));
            outList.addParameter(TInteger.Create(LOG_LAYER));
            outList.addParameter(TString.Create((newname.String_)));
            aMaster.ProcessObject(TProcessItem.create(PO_SCENE_UPDATE_LAYER, outList));
            Project.updateLogLayer(name.String_, newname.String_);
            aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_WITH_PATH, NIL));
            Result := TProcessItem.create(PO_SUCCESS, nil);
          end
        else Result := TProcessItem.create(PO_FAILURE, nil);
      end;

      PO_PROJECT_INSERT_PHYS_LAYER: begin
         aProject.addPhysLayer(TString(inList.Parameters[0]).String_);
         aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_WITH_PATH, NIL));
         Result := TProcessItem.create(PO_SUCCESS, nil);
         outList :=TProcessingList.Create;
         outList.addParameter(TString.Create
          (Project.getPhysLayerFileName(TString(inList.Parameters[0]).String_)));
         outList.addParameter(TInteger.Create(oLevel.PHYS_LAYER));
         outList.addParameter(inList.Parameters[0]);
         if inList.Parameters.Count = 4 then
          begin
           outList.addParameter(inList.Parameters[1]);
           outList.addParameter(inList.Parameters[2]);
           outList.addParameter(inList.Parameters[3]);
          end;
         aMaster.ProcessObject(TProcessItem.create(PO_SCENE_UPDATE_LAYER,
                               outList));
      {   aMaster.ProcessObject(TProcessItem.create(
         PO_PROJECT_SAVE_PHYS_LAYER, TString.Create(
          TString(inList.Parameters[0]).String_)));}
      end;

      PO_PROJECT_SAVE_PHYS_LAYER : begin
         aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_WITH_PATH, NIL));
         outList := TProcessingList.Create;
         outList.addParameter(TInteger.Create(oLevel.PHYS_LAYER));
         outList.addParameter(TString.Create(
         TProject.getFileName(name.String_, Project.PhysicalLayers)));
         aMaster.ProcessObject(TProcessItem.create(PO_SCENE_SAVE_ACTUAL_LAYER, outlist));
      end;

      PO_PROJECT_SAVE_LOG_LAYER : begin
         aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_WITH_PATH, NIL));
         outList := TProcessingList.Create;
         outList.addParameter(TInteger.Create(oLevel.LOG_LAYER));
         outList.addParameter(TString.Create(
         TProject.getFileName(name.String_, Project.LogicalLayers)));
         aMaster.ProcessObject(TProcessItem.create(PO_SCENE_SAVE_ACTUAL_LAYER, outlist));
      end;
      PO_PROJECT_INSERT_LOG_LAYER : begin
         aProject.addLogLayer(name.String_);
         aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_WITH_PATH, NIL));
         inList := TProcessingList.Create;
         inList.addParameter(name);
         inList.addParameter(TInteger.Create(oLevel.LOG_LAYER));
         aMaster.ProcessObject(TProcessItem.create(PO_SCENE_INSERT_LAYER,
                               inList));

         aMaster.ProcessObject(TProcessItem.create(
         PO_PROJECT_SAVE_LOG_LAYER, TString.Create(
          name.String_)));
         aMaster.ProcessObject(TProcessItem.create(
         PO_SCENE_LOAD_LOG_LAYER, TString.Create(name.String_)));

      end;

      PO_PROJECT_SAVE : begin
        item := aMaster.ProcessObject(TProcessItem.create(PO_SCENE_GET_ID, NIL));
        aProject.EntityID := TInteger(item.Data).Integer_;
        aProject.SaveToXML(name.String_);
        aMaster.ProcessObject(TProcessItem.create(PO_SCENE_SET_PATH, TString.Create(Project.Path)));
        saveAll;
        aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_WITH_PATH, NIL));
//        aMaster.ProcessObject(TProcessItem.create(PO_PATHS_SAVE, nil));
      end;

      PO_PROJECT_SAVE_WITH_PATH : begin
        item := aMaster.ProcessObject(TProcessItem.create(PO_SCENE_GET_ID, NIL));
        aProject.EntityID := TInteger(item.Data).Integer_;
        aProject.SaveToXML(aProject.Path);
        aMaster.ProcessObject(TProcessItem.create(PO_SCENE_SET_PATH, TString.Create(Project.Path)));
      end;

      PO_PROJECT_LOAD : begin
        aProject.Free;
        aProject := TProject.Create;
        aProject.LoadFromXML(name.String_);
        aMaster.ProcessObject(TProcessItem.create(PO_SCENE_SET_PATH, TString.Create(Project.Path)));
        aMaster.ProcessObject(TProcessItem.create(PO_TREE_SHOW_PROJECT,
        TString.Create(Project.Name)));

        with Project do
         begin
           insert(PhysicalLayers, PO_TREE_INSERT_PHYS_LAYER_FAST);
           insert(Pedestrians, PO_TREE_INSERT_PEDEST_LAYER_FAST);
           insert(LogicalLayers, PO_TREE_INSERT_LOG_LAYER_FAST);
           insert(Paths, PO_TREE_INSERT_PATHS_LAYER_FAST);
           insert(ArrivalGroups, PO_TREE_INSERT_ARRIVALS_LAYER_FAST);
           //loadDataFromManager(PO_PROJECT_LOAD_ARRIVALS, PO_ARRMAN_GET_NAMES, PO_TREE_INSERT_ARRIVALS_LAYER_FAST);
           loadDataFromManager(PO_PROJECT_LOAD_CONFIGS, PO_CONFIG_GET_NAMES, PO_TREE_INSERT_CONFIG_LAYER_FAST);
           loadDataFromManager(PO_PROJECT_LOAD_RUN_PROPS, PO_RUNPROPMAN_GET_NAMES, PO_TREE_INSERT_RUNPROP_LAYER_FAST);
         end;
        result := aMaster.ProcessObject(TProcessItem.create(PO_SCENE_SET_ID,
        TInteger.Create(aProject.EntityID)));
      end;

      PO_PROJECT_INSERT_PEDESTRIANS : begin
         Project.addPedestrian(name.String_);
         aPedName := name.String_;
         aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_WITH_PATH, NIL));
         Result := aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_PEDESTRIANS, NIL));
      end;

      PO_PROJECT_DELETE_PEDESTRIANS : begin
        Project.deletePedestrian(name.String_);
        aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_WITH_PATH, NIL));
        aPedName := '';
      end;

      PO_PROJECT_ARRIVALS_NAME : begin
        slist := TStringList.Create;
        for map in Project.Arrivals do
           slist.Add(map.Key);
        Result := TProcessItem.create(PO_SUCCESS, TProcessingStrings.Create(slist));
      end;

      PO_PROJECT_INSERT : begin
        if aProject <> nil then
          aProject.Free;
        aProject := TProject.Create;
        aProject.Name := name.String_;
        Result := TProcessItem.create(PO_SUCCESS, NIL);
      end;

      PO_PROJECT_SAVE_PEDESTRIANS : begin
        if aPedName <> '' then
          Result := saveDataFromManager(PO_PEDMAN_SAVE, '\pedestrians\' +
          project.getPedestrianFileName(aPedName) + '.xml')
        else
          Result := nil;
      end;
      PO_PROJECT_LOAD_PEDESTRIANS : begin
          item := aMaster.ProcessObject(TProcessItem.create(PO_LOADMAN_LOAD_DATA,
          TString.Create(aProject.Path + '\pedestrians\' +
            Project.getPedestrianFileName(name.String_) + '.xml')));

          if item.Code = PO_SUCCESS then
           begin
            aPedName := name.String_;
            aMaster.ProcessObject(TProcessItem.create(PO_PEDMAN_LOAD, TProcessingXML(item.Data)))

           end
          else raise EProjectManager.Create('Error on load pedestrians');
      end;

      PO_PROJECT_INSERT_PATHS : begin
        Project.addPath(name.String_);
        aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_WITH_PATH, NIL));
        aPathName := name.String_;
        aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_PATHS, nil));

        Result := TProcessItem.create(PO_SUCCESS, NIL);
      end;

      PO_PROJECT_DELETE_PATHS : begin
         Project.deletePath(name.String_);
         aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_WITH_PATH, NIL));
         Result := TProcessItem.create(PO_SUCCESS, NIL);
      end;

      PO_PROJECT_INSERT_ARRIVALS_GROUP : begin
         Project.addArrivalGroup(name.String_);
         item := aMaster.ProcessObject(TProcessItem.create(PO_SAVEMAN_PREPARE_SAVE, nil));
         if not DirectoryExists(Project.Path + '\arrivals\') then
            MkDir(Project.Path + '\arrivals\');
         if not DirectoryExists(Project.Path + '\arrivals\' + name.String_) then
            MkDir(Project.Path + '\arrivals\' + Project.getArrivalGroupFileName(name.String_));
         outList := TProcessingList.Create;
         outList.addParameter(TString.Create(Project.Path + '\arrivals\' +
         Project.getArrivalGroupFileName(name.String_) + '.xml'));
         outList.addParameter(item.Data);
         aMaster.ProcessObject(TProcessItem.create(PO_SAVEMAN_SAVE_DATA, outList));
         aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_WITH_PATH, NIL));
         Result := nil;
      end;

      PO_PROJECT_INSERT_ARRIVAL : begin
         if aArrivalGroup <> '' then
          begin
           Project.addArrival(name.String_, aArrivalGroup);
           aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_WITH_PATH, NIL));
           Result := aMaster.ProcessObject(TProcessItem.create(
            PO_PROJECT_GET_ARRIVAL_XML, TString.Create(name.String_)));
          end
         else
          begin
            raise EProjectManager.Create('Arrival group not set');
            Result := TProcessItem.create(PO_FAILURE, nil);
          end;
      end;

      PO_PROJECT_DELETE_ARRIVALS_GROUP : begin
         aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_LOAD_ARRIVALS_GROUP,
         TString.Create(name.String_)));
         Project.deleteArrivalGroup(name.String_);
         aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_WITH_PATH, NIL));
         Result := nil;
      end;


      PO_PROJECT_DELETE_ARRIVAL : begin
         if aArrivalGroup <> '' then
          begin
           Project.deleteArrival(name.String_, aArrivalGroup);
           Result := TProcessItem.create(PO_SUCCESS, NIL);
          end
         else
          begin
            raise EProjectManager.Create('Arrival group not set');
          end;
      end;

      PO_PROJECT_GET_LAYER_PATH : begin
        name := TString(inList.Parameters[0]);
        value := TInteger(inList.aParameters[1]);
        if value.Integer_ = oLevel.PHYS_LAYER then
          Result := TProcessItem.create(PO_SUCCESS, TString.Create(
              Project.getPhysLayerFileName(name.String_)))
        else
          Result := TProcessItem.create(PO_SUCCESS, TString.Create(
              Project.getLogLayerFileName(name.String_)));
      end;

      PO_PROJECT_CLOSE_XML : begin
         inList := TProcessingList.Create;
         inList.addParameter(TString.Create(Project.Path + '\arrivals\' +
          Project.getArrivalGroupFileName(aArrivalGroup) + '\' +
          Project.getArrivalFileName(aArrival) + '.xml'));
         inList.addParameter(paItem.Data);
         aMaster.ProcessObject(TProcessItem.create(PO_SAVEMAN_SAVE_DATA, inList));
         Result := nil;
      end;

      PO_PROJECT_SAVE_ARRIVALS : begin
         Result := saveDataFromManager(PO_ARRMAN_SAVE, '\arrivals\arrivals.xml');
      end;

      PO_PROJECT_SAVE_CONFIGS : begin
        Result := saveDataFromManager(PO_CONFIG_SAVE, '\configs\configs.xml');
      end;

      PO_PROJECT_SAVE_RUN_PROPS : begin
        Result := saveDataFromManager(PO_RUNPROPMAN_SAVE, '\run_props\run_props.xml');
      end;

      PO_PROJECT_SAVE_PATHS : begin
        if aPathName <> '' then
          Result := saveDataFromManager(PO_PATHS_SAVE, '\paths\' +
             Project.getPathFileName(aPathName) + '.xml')
        else
          Result := nil;
      end;

      PO_PROJECT_LOAD_RUN_PROPS : begin
        item := aMaster.ProcessObject(TProcessItem.create(PO_LOADMAN_LOAD_DATA, TString.Create(aProject.Path + '\run_props\run_props.xml')));
        if item.Code = PO_SUCCESS then
          aMaster.ProcessObject(TProcessItem.create(PO_RUNPROPMAN_LOAD, TProcessingXML(item.Data)))
        else raise EProjectManager.Create('Error on load run properties');

      end;

      PO_PROJECT_LOAD_CONFIGS : begin
        item := aMaster.ProcessObject(TProcessItem.create(PO_LOADMAN_LOAD_DATA, TString.Create(aProject.Path + '\configs\configs.xml')));
        if item.Code = PO_SUCCESS then
          aMaster.ProcessObject(TProcessItem.create(PO_CONFIG_LOAD, TProcessingXML(item.Data)))
        else raise EProjectManager.Create('Error on load configuration');

      end;

      PO_PROJECT_UNLOAD_ARRIVALS_GROUP : begin
        Project.Arrivals.Clear;
        aArrivalGroup := '';
      end;

      PO_PROJECT_GET_ARRIVAL_XML : begin
          aArrival := name.String_;
          item := aMaster.ProcessObject(TProcessItem.create(PO_SAVEMAN_PREPARE_SAVE, nil));
          Result := TProcessItem.create(PO_SUCCESS, TProcessingXML.Create((item.Data as TProcessingXML).Node));
      end;

      PO_PROJECT_LOAD_ARRIVAL : begin
        if (aArrivalGroup <> '') then
          begin
           item := aMaster.ProcessObject(TProcessItem.create(PO_LOADMAN_LOAD_DATA,
               TString.Create(Project.Path + '\arrivals\' +
               Project.getArrivalGroupFileName(aArrivalGroup) + '\' +
                Project.getArrivalFileName(name.String_) + '.xml')));
           if item.Code <> PO_FAILURE then
            Result := aMaster.ProcessObject(TProcessItem.create(PO_ARRMAN_LOAD, TProcessingXML(item.Data)))
          else
            raise EProjectManager.Create('Error on load arrivals');
          end
        else
          raise EProjectManager.Create('Arrival group not set');
      end;

      PO_PROJECT_LOAD_ARRIVALS_GROUP : begin
        Project.LoadArrivals(name.String_);
        aArrivalGroup := name.String_;
      end
   else
      raise EProjectManager.Create('Project Manager: Could not process message with code' + IntToStr(paItem.Code));
      Result := TProcessItem.create(PO_FAILURE, nil);
    end;

   paItem.Free;
end;

procedure TProjectManager.saveAll;
begin
  //aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_ARRIVALS, nil));
  aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_CONFIGS, NIL));
  aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_RUN_PROPS, NIL));

  aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_SCENE, NIL));

//  aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_PEDESTRIANS, nil));
 // aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_PATHS, NIL));
end;

function TProjectManager.saveDataFromManager(paSaveCode : integer; paPath : string) : TProcessItem;
var
  item : TProcessItem;
  inList : TProcessingList;
begin
  item := aMaster.ProcessObject(TProcessItem.create(PO_SAVEMAN_PREPARE_SAVE, nil));
  aMaster.ProcessObject(TProcessItem.create(paSaveCode,
    item.Data));
    if item.Data is TProcessingXML then
     begin
      inList := TProcessingList.Create;
      inList.addParameter(TString.Create(Project.Path + paPath));
      inList.addParameter(item.Data);
      aMaster.ProcessObject(TProcessItem.create(PO_SAVEMAN_SAVE_DATA, (inList)));
      Result := TProcessItem.create(PO_SUCCESS, nil);
     end
    else
      Raise EProjectManager.Create('Expected Tprocessing XML for paths save');
end;

end.



